Ext.define('BenBen.modules.system.DepartmentModule',{
    extend : 'Ext.ux.desktop.Module',
    requires: [
        'BenBen.common.BasePanel',
        'BenBen.common.component.TreePicker'
    ],
    id : 'department-win',
    border : false,
    init : function(){
        this.launcher = {
            text: '部门管理',
            iconCls:'icon-grid'
        };
    },
    createWindow : function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('department-win');

        if (!win) {
            var dataModelFields = ['_id','departmentName', 'upDepartment.departmentName', 'type',  'remark', 'status', 'departmentNo'];

            var columns = [{
                xtype: 'rownumberer',
                width: 40,
                sortable: false,
                locked: true
            },{
                dataIndex: 'departmentName',
                text: '部门名称'
            },{
                dataIndex: 'departmentNo',
                text: '部门编号'
            },{
                dataIndex: 'upDepartment.departmentName',
                text: '上级部门'
            },{
                dataIndex: 'type',
                text: '类型',
                renderer: function (value) {
                    if (value == '0') {
                        return "总公司";
                    }else if (value == '1') {
                        return "普通部门";
                    } else if (value == '2') {
                        return "行业组";
                    } else if (value == '3') {
                        return "区域办事处";
                    }
                }
            },{
                dataIndex: 'remark',
                text: '备注'
            },{
                dataIndex: 'status',
                text: '状态',
                renderer: function (value) {
                    if (value == '1') {
                        return "有效";
                    } else if (value == '2') {
                        return "停用";
                    }
                }
            }];

            var searchItem = [{
                xtype: 'textfield',
                name: 'departmentName',
                isSearch: true,
                fieldLabel: '部门名称',
                labelAlign: 'right'
            },{
                xtype: 'textfield',
                name: 'departmentNo',
                isSearch: true,
                fieldLabel: '部门编号',
                labelAlign: 'right'
            }];

            Ext.define('model.department', {
                extend: 'Ext.data.Model',
                fields: [{
                    name: 'departmentName'
                },{
                    name: 'upDepartment'
                },{
                    name: 'type'
                },{
                    name: 'createTime'
                },{
                    name: 'remark'
                },{
                    name: 'status'
                },{
                    name: 'departmentNo'
                }]
            });

            var dataForm = Ext.create('Ext.form.Panel', {
                border : false,
                bodyStyle : 'padding:5px',
                layout : 'anchor',
                defaults : {
                    layout : 'column',
                    margin: '5 5 5 5',
                    anchor : '90%',
                    labelWidth : 60,
                    msgTarget :'side',
                    labelAlign : 'right'
                },
                reader :  Ext.create('Ext.data.JsonReader', {
                    root : 'data',
                    model : 'model.department'
                }),
                items : [{
                    xtype: 'textfield',
                    name: 'departmentName',
                    allowBlank: false,
                    blankText : '部门名称不能为空',
                    fieldLabel: '部门名称'
                },{
                    xtype: 'textfield',
                    name: 'departmentNo',
                    fieldLabel: '部门编号'
                }, {
                    xtype: 'treepicker',
                    name: 'upDepartment',
                    fieldLabel: '上级部门',
                    idField : '_id',
                    displayField: 'departmentName',
                    sumbitDisplayField : true,
                    minPickerHeight: 200,
                    store: Ext.create('Ext.data.TreeStore',{
                        fields: ['_id','departmentName'],
                        root: {
                            departmentName: 'root',
                            expanded: true
                        },
                        proxy: {
                            type: 'rest',
                            method : 'GET',
                            url: '/systemDepartment/tree',
                            reader: {
                                type: 'json'
                            }
                        }
                    })
                },{
                    xtype: 'combo',
                    name: 'type',
                    fieldLabel: '类型',
                    mode : 'local',
                    editable : false,
                    value : 1,
                    triggerAction : 'all',
                    displayField : 'name',
                    valueField : 'number',
                    store : new Ext.data.SimpleStore({
                        fields : ['number','name'],
                        data : [[0,'总公司'],[1,'普通部门'],[2,'行业组'],[3,'区域']]
                    })
                },{
                    xtype: 'textarea',
                    name: 'remark',
                    fieldLabel: '备注'
                }]
            });

            var departmentPanel = Ext.create('BenBen.common.BasePanel', {
                columns : columns,
                searchItem : searchItem,
                dataForm : dataForm,
                dataModelFields : dataModelFields,
                tbarDefaultItems : 'add,update,look',
                tbarItems : [{
                    text : '停用',
                    iconCls : 'griddelete',
                    handler : function(){
                        Ext.MessageBox.confirm('信息', '如果停用此部门，其子部门也会停用。你确定要停用此部门吗?', function(btn) {
                            if (btn == 'yes') {
                                var record = departmentPanel.getSeletedRecord();
                                if(record != null){
                                    var url = departmentPanel.config.url;
                                    url += '/stop/' + record.get('_id');
                                    Ext.Ajax.request({
                                        url: url,
                                        method: 'PUT',
                                        callback: function (options, success, response) {
                                            if (success) {
                                                var msg = Ext.decode(response.responseText);
                                                if(msg.success){
                                                    Ext.Msg.confirm('成功',msg.info);
                                                    departmentPanel.reload();
                                                }else{
                                                    Ext.Msg.confirm('失败',msg.info);
                                                }

                                            } else {
                                                Ext.Msg.confirm('失败','操作失败');
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                }],
                config : {
                    url : '/systemDepartment/departments',
                    pageSize : 30,
                    winWidth : 300,
                    winHeigth : 260
                }
            });
            win = desktop.createWindow({
                id: 'department-win',
                title: '部门管理',
                width: 800,
                height: 480,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'fit',
                items: [departmentPanel]
            });
        }


        return win;
	}
});
